/**
 * Copyright 2009 Henning Teek, Ingo Renner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.bricket.plugin.user.web;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.odlabs.wiquery.ui.themes.ThemeUiHelper.IconEnum;

import com.googlecode.bricket.plugin.user.domain.User;
import com.googlecode.bricket.plugin.user.repository.UserService;
import com.googlecode.bricket.web.CaptchaPanel;
import com.googlecode.bricket.web.common.BricketAjaxButton;
import com.googlecode.bricket.web.common.BricketAjaxLink;
import com.googlecode.bricket.web.common.BricketPanel;

/**
 * @author Henning Teek
 */
public class SignUpPanel extends BricketPanel {
	@SpringBean
	UserService userService;

	private final Form<User> form;

	private final CaptchaPanel captchaPanel;

    public SignUpPanel(String id) {
		this(id, new User());
	}

	public SignUpPanel(String id, User user) {
		super(id);
		CompoundPropertyModel<User> model = new CompoundPropertyModel<User>(user);
		form = new Form<User>("signup.form", model);
		form.add(new AccountPanel("account", model, form));
		captchaPanel = new CaptchaPanel("captcha");
		form.add(captchaPanel);
		add(form);

		setHeadline(new StringResourceModel("signup.headline", SignUpPanel.this, null));

		addButton(new BricketAjaxLink(new StringResourceModel("cancel.label", SignUpPanel.this, null), IconEnum.CANCEL) {
			@Override
			public void onClick(AjaxRequestTarget target) {
				setResponsePage(getApplication().getHomePage());
			}
		});
		
		addButton(new BricketAjaxButton(new StringResourceModel("submit.label", SignUpPanel.this, null), IconEnum.PENCIL, form) {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				if (captchaPanel.isCaptchaValid()) {				
					userService.signupUser((User) form.getModelObject());
					getSession().info(new StringResourceModel("feedback.success", SignUpPanel.this, null).getString());
					setResponsePage(getApplication().getHomePage());
				} else {
					form.error(new StringResourceModel("feedback.captcha", SignUpPanel.this, null).getString());
					target.addComponent(getFeedback());
				}
			}
			
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				target.addComponent(getFeedback());
			}
		});
	}
}
